//
//  WBTitleButton.swift
//  WeiBoApp
//
//  Created by James on 2017/7/13.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit

class WBTitleButton: UIButton {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    ///重载够着函数
    
    init(title:String?) {
        super.init(frame: CGRect())
        
        if title == nil {
            setTitle("首页", for: [])
        }else{
            setTitle(title!+" ", for: [])
            setImage(UIImage(named:"navigationbar_arrow_down"), for: [])
            setImage(UIImage(named:"navigationbar_arrow_up"), for: .selected)
        }
        
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: [])
        sizeToFit()
        layoutSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //重新布局子试图
    override func layoutSubviews() {
        super.layoutSubviews()
        
        print("调整按钮布局位置")
        
        guard let titleLabel = titleLabel,
              let imageView = imageView else {
            return
        }
        //将label的x左移动  imageView向右移动
        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
        
        imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width, dy: 0)
        
    }
    
    
}
